//
//  Extra.m
//  家校互通
//
//  Created by 姬鑫宇 on 15/4/20.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "Extra.h"

@implementation Extra

+ (NSURLSessionDataTask *)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block withSn:(NSString *)sn andPhone_no:(NSString *)phone_no{
    NSDictionary * dict = @{@"op": @"current", @"sn":@"862492011026735",@"phone_no":@"13564701425"};
    return [[AFAppDotNetAPIClient sharedClient] POST:@"mobile_points/" parameters:dict success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@",task.originalRequest);
        NSArray *postsFromResponse = [JSON valueForKeyPath:@"extra"];
        NSMutableArray *mutablePosts = [NSMutableArray arrayWithCapacity:[postsFromResponse count]];
        for (NSDictionary *attributes in postsFromResponse) {
            NSError *error;
            Extra *post = [[Extra alloc] initWithDictionary:attributes error:&error];
            [mutablePosts addObject:post];
        }
        
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}

+ (NSURLSessionDataTask *)getMorePosWithBlock:(void (^)(NSString *resultStr, NSArray *posts, NSError *error))block withSNS:(NSArray *)snsArray{
    NSString *snsString = @"";
    for (NSString *string in snsArray) {
        snsString  = [snsString stringByAppendingString:string];
        if (![string isEqualToString:[snsArray lastObject]]) {
            snsString  = [snsString stringByAppendingString:@","];
        }
    }
    NSDictionary * dict = @{@"sns": snsString};
    return [[AFAppDotNetAPIClient sharedClient] POST:@"getMorePos" parameters:dict success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@",task.originalRequest);
        NSString *result = [JSON valueForKeyPath:@"result"];
        NSMutableArray *mutablePosts = [[NSMutableArray alloc]initWithCapacity:0];

        if ([result isEqualToString:@"ok"]) {
            NSArray *postsFromResponse = [JSON valueForKeyPath:@"positions"];
            for (NSDictionary *attributes in postsFromResponse) {
                NSError *error;
                ExtraPostion *post = [[ExtraPostion alloc] initWithDictionary:attributes error:&error];
                [mutablePosts addObject:post];
            }
        }
        
        if (block) {
            block(result,[NSArray arrayWithArray:mutablePosts], nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        NSLog(@"%@",task.originalRequest);
        if (block) {
            block(nil,[NSArray array], error);
        }
    }];
}



@end

@implementation ExtraPostion @end
